import * as index from '@/api/index.js'
import * as user from '@/api/user.js'
import Recruit from '@/components/home/recruit/index.vue'
import Work from '@/components/home/work/index.vue'
import Swiper from '@/components/home/swiper/index.vue'
import Category from '@/components/home/category/index.vue'
import InputBtn from '@/components/home/inpbtn/index.vue'
import DropDown from '@/components/home/DropDown/index.vue'
import ImageLabel from '@/components/home/imageLabel/index.vue'
import {
	mapMutations
} from 'vuex';
export default {
	components: {
		Recruit,
		Work,
		Swiper,
		Category,
		InputBtn,
		DropDown,
		ImageLabel
	},
	data() {
		return {
			resume_forward: '', //增加次数
			scrollTop: 0,
			status: 'more',
			title: '', //分享标题
			imgage: '', //分享图片
			userInfo: {},
			Type: uni.getStorageSync('Type') ? uni.getStorageSync('Type') : 0,
			total: -1,
			form: {
				page: 1,
				pages: '',
				neqId: '',
				select_name: '',
				member_id: '',
				drving_id: '',
				age_id: '',
				welfare_id: '',
				salary_id: '',
				position_id2: '',
				position_id: '',
				areas_pid: '',
				areas_id: '',
				secondary: '',
				is_index: '',
				gender: 2,
				refresh_time: '', //更新时间
				date: ''
			},
			workList: [], //需求列表
			typeList: [], //热门需求
			resumeList: [], //接单列表
			versionCode: '', //版本后
			// unreadTotal:''
		}
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop
	},
	onShow() {
		// this.setbadgr()
		//#ifdef APP-PLUS
		plus.push.clear();
		plus.runtime.setBadgeNumber(-1)
		// #endif
		if (uni.getStorageSync('Type') == 2) {
			uni.setStorage({
				key: 'Type',
				data: 1,
				success: res => {
					this.Type = 1
				}
			});
		}
		if (uni.getStorageSync('Type') === 1) {
			if (this.form.page == 1) {
				this.getResumeList()
			}
			uni.setTabBarItem({
				index: 0,
				text: '牛人',
			})
			uni.setTabBarItem({
				index: 1,
				text: '发布需求',
			})
		} else {
			if (this.form.page == 1) {
				this.getPosotionList()
				this.getPidPositionTypeList()
			}
			uni.setTabBarItem({
				index: 0,
				text: '找需求',
			})
			uni.setTabBarItem({
				index: 1,
				text: '发布接单',
			})
		}
		if (uni.getStorageSync('token')) {
			this.nameGetConfig()
			this.getUserInfo()
		}
	},
	onLoad(option) {
		this.toUptate()
		uni.$on('Type', (data) => {
			this.Type = uni.getStorageSync('Type')
			if (uni.getStorageSync('token')) {
				this.getUserInfo()
			}
		})
		if (option.type == '0') {
			console.log('接单');
			this.qiuzhi()
		} else if (option.type == '1') {
			console.log('需求');
			this.zhaopin()
		}
	},
	onReady() {
		if (uni.getStorageSync('Type') === '') {
			this.$refs.popup.open()
		}
	},
	onReachBottom() {
		this.form.page += 1
		if (uni.getStorageSync('Type') == 0) {
			this.getPosotionList()
		} else {
			this.getResumeList()
		}
	},
	onPullDownRefresh() {
		this.form.page = 1
		if (uni.getStorageSync('Type') == 0) {
			this.workList = []
			this.getPosotionList()
		} else {
			this.resumeList = []
			this.getResumeList()
		}
	},
	computed: {
		monitor() {
			return this.$store.state.Type
		}
	},
	watch: {
		monitor(newval, oldval) {

		}
	},
	onShareAppMessage(res) {
		if (res.from === 'button') { // 来自页面内分享按钮
			user.wxForwardGetAward({
				type: 0
			}).then(res => {
				// 关闭弹窗
			})
		}
		return {
			title: this.title, //分享标题
			imageUrl: this.imgage,
			path: '/pages/home/index',
		}
	},
	methods: {
		...mapMutations(['converList', 'setRoleId']),
		// 获取分享图片
		nameGetConfig() {
			index.nameGetConfig({
				name: 'wx_share_image,wx_share_title,resume_forward'
			}).then(res => {
				this.imgage = res.data.wx_share_image
				this.title = res.data.wx_share_title
				this.resume_forward = res.data.resume_forward
				if (uni.getStorageSync('shareType') == 1) {
					uni.showToast({
						icon: 'none',
						title: `查看需求已+${res.data.resume_forward}条,\n感谢您助力鱼台就业。`,
						duration: 2000
					});
					uni.removeStorageSync('shareType');
				}
			})
		},
		getUserInfo() {
			let that = this
			user.getUserInfo({
				type: this.Type
			}).then(res => {
				this.userInfo = res.data
				uni.setStorage({
					key: 'userInfo',
					data: res.data,
					success: function() {
						uni.$emit('userin', res.data)
						if (that.goEasy.getConnectionStatus() == 'disconnected') {
							that.connect()
						} else {
							that.latestConversations()
						}
						that.listenConversationUpdate()
					}
				});
			})
		},
		getPosotionList() {
			this.status = 'loading'
			index.getPosotionList(this.form).then(res => {
				if (this.form.page == 1) {
					this.workList = res.data.data
				} else {
					this.workList.push(...res.data.data)
				}
				this.total = res.data.total
				setTimeout(() => {
					if (res.data.current_page < res.data.last_page) {
						this.status = 'more'
					} else {
						this.status = 'noMore'
					}
				}, 200)
			})
		},


		//DropDown传值
		getForm(e) {
			console.log(e.position_id,'eeeeeeeeeeeee');
			this.form.areas_id = e.areas_id
			this.form.age_id = e.age_id
			this.form.drving_id = e.drving_id
			this.form.position_id = e.position_id
			this.form.position_id2 = e.position_id2
			this.form.salary_id = e.salary_id
			this.form.secondary = e.secondary
			this.form.gender = e.gender
			this.form.refresh_time = e.refresh_time
			this.form.date = e.date
			this.form.welfare_id = e.welfare_id.join(',')
			this.form.page = 1
			this.workList = []
			this.resumeList = []
			if (this.Type == 0) {
				this.getPosotionList()
			} else {
				this.getResumeList()
			}
		},
		//获取接单列表
		getResumeList() {
			this.status = 'loading'
			var data = this.form
			data.position_id = this.form.position_id2
			let form = JSON.parse(JSON.stringify(data))
			index.getResumeList(form).then(res => {
				if (this.form.page == 1) {
					this.resumeList = res.data.data
				} else {
					this.resumeList.push(...res.data.data)
				}
				this.total = res.data.total
				setTimeout(() => {
					if (res.data.current_page < res.data.last_page) {
						this.status = 'more'
					} else {
						this.status = 'noMore'
					}
				}, 200)
			})
		},
		//需求类别
		getPidPositionTypeList() {
			index.getPidPositionTypeList({
				index: 1
			}).then(res => {
				res.data.unshift({
					id: 1,
					title: '兼职',
					type: 'secondary'
				})
				this.typeList = res.data
			})
		},
		//链接goeazy
		connect() {
			let userInfo = uni.getStorageSync('userInfo')
			console.log(userInfo.gid);
			//链接im
			// return;
			if (this.goEasy.getConnectionStatus() == 'disconnected') {
				this.goEasy.connect({
					id: userInfo.gid,
					data: {
						nickname: userInfo.name,
						avatar: userInfo.head
					},
					onSuccess: () => {
						console.log('连接goeasy');
						this.latestConversations()
					},
					onFailed: (error) => {},
					onProgress: (attempts) => {}
				});
			}
		},
		listenConversationUpdate() { // 监听会话列表变化
			this.goEasy.im.on(this.GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, (content) => {
				// console.log(content);
				this.unreadTotal = content.unreadTotal
				// this.setbadgr()

				this.converList(content.conversations)
			});
		},
		latestConversations() { //获取最新的会话列表
			// uni.showLoading();
			this.goEasy.im.latestConversations({
				onSuccess: (result) => {
					let content = result.content;
					this.converList(content.conversations)
					// this.renderConversations(content);
				},
				onFailed: (error) => {}
			});
		},
		toUptate() {
			//#ifdef APP-PLUS 
			plus.runtime.getProperty(plus.runtime.appid, (inf) => {
				this.versionCode = inf.versionCode
			})
			index.renew({
				type: uni.getSystemInfoSync().platform, //android或者ios
			}).then(res => {
				if (uni.getSystemInfoSync().platform == 'android') {
					if (parseInt(this.versionCode) < parseInt(res.data.edition_number)) {
						uni.navigateTo({
							url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
								JSON.stringify(res.data)
						});
					}
				} else {
					if (parseInt(this.versionCode) < parseInt(res.data.edition_number)) {
						setTimeout(() => {
							uni.navigateTo({
								url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
									JSON.stringify(res.data)
							});
						}, 1000)
					}
				}
			})
			//#endif    
		},
		scroll() {
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 200
			})
		},
		change(type) {
			this.$tools.change(type)
		},
		change1() {
			this.$tools.change(0)

		},
		change2() {
			this.$tools.change(1)
		},
		qiuzhi() {
			this.$tools.change(1)
		},
		zhaopin() {
			this.$tools.change(0)
		}
	}
}
